package com.yonyou.framework.library.common.assist;

import com.yonyou.framework.library.common.log.Elog;

import java.util.ArrayList;

/**
 * 作者：joseph_l on 8/1/16 10:01
 * 邮箱：ljt@yonyou.com
 * 均值器， 添加一些列数字或时间戳，获取其均值
 */
public class Averager{

private static final String TAG = Averager.class.getSimpleName();
private ArrayList<Number> numList = new ArrayList<Number>();

        /**
         * 添加一个数字
         *
         * @param num
         */
        public synchronized void add(Number num) {
            numList.add(num);
        }

        /**
         * 清除全部
         */
        public void clear() {
            numList.clear();
        }

        /**
         * 返回参与均值计算的数字个数
         *
         * @return
         */
        public Number size() {
            return numList.size();
        }

        /**
         * 获取平均数
         *
         * @return
         */
        public Number getAverage() {
            if (numList.size() == 0) {
                return 0;
            } else {
                Float sum = 0f;
                for (int i = 0, size = numList.size(); i < size; i++) {
                    sum = sum.floatValue() + numList.get(i).floatValue();
                }
                return sum / numList.size();
            }
        }

        /**
         * 打印数字列
         *
         * @return
         */
        public String print() {
            String str = "PrintList(" + size() + "): " + numList;
            Elog.d(TAG,str);
            return str;
        }

}

